package com.itshops.controller;

import com.itshops.entity.PageBean;
import com.itshops.entity.Product;
import com.itshops.service.ProductService;
import com.itshops.service.impl.ProductServiceImpl;
import com.itshops.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@WebServlet("/product")
public class ProductController extends BaseServlet{
    ProductService productService=new ProductServiceImpl();
    public String show(HttpServletRequest req,HttpServletResponse resp) throws SQLException {
        //接收请求参数tId,获取页容量和当前页数
        String tId = req.getParameter("tId");
        String currentPage = req.getParameter("currentPage");
        int pagesize=6;
        int page=1;//默认在第一页
        if (currentPage!=null){
            //当前页数
            page=Integer.parseInt(currentPage);
        }
        //调用前端需要展示的PageBean属性
        PageBean<Product> pageBean=productService.findPage(tId,page,pagesize);
        //响应
        req.setAttribute("pageBean",pageBean);
        return Constants.FORWARD+"/goodsList.jsp";
    }

    //展示商品详情信息
    public String detail(HttpServletRequest req,HttpServletResponse resp) throws SQLException {
        //获取参数
        String pId = req.getParameter("pId");
        //调用业务逻辑
        Product product=productService.fiandProductByPid(pId);
        //响应
        req.setAttribute("product",product);
        return Constants.FORWARD+"/goodsDetail.jsp";
    }
}
